/*
2017-04-25
*/
var ROOT='http://api.hongyanche.com';
CONSOLE = {
	urls:{
		label_rank		: ROOT+"/mall_admin/labels",
		label_modify	: ROOT+"/mall_admin/label_modify",

		config_list		: ROOT+"/mall_admin/config_getlist",
		config_create	: ROOT+"/mall_admin/config_create",
		config_remove	: ROOT+"/mall_admin/config_remove",
		config_release  : ROOT+"/mall_admin/config_release",
		config_range	: ROOT+"/mall_admin/config_range",
		config_rename	: ROOT+"/mall_admin/config_rename",

		config_option_create	: ROOT+"/mall_admin/config_option_create",
		config_option_remove	: ROOT+"/mall_admin/config_option_remove",
		config_option_clean		: ROOT+"/mall_admin/config_option_clean",
		config_option_range		: ROOT+"/mall_admin/config_option_range",

		brand_list		: ROOT+"/mall_admin/product_getlist",
		brand_create	: ROOT+"/mall_admin/product_create",
		brand_remove	: ROOT+"/mall_admin/product_remove",
		brand_release   : ROOT+"/mall_admin/product_release",
		brand_range		: ROOT+"/mall_admin/product_range",
		brand_modify	: ROOT+"/mall_admin/product_modify",
		brand_highlight	: ROOT+"/mall_admin/product_highlight",

		detail_list		: ROOT+"/mall_admin/detail_getlist",
		detail_create	: ROOT+"/mall_admin/detail_create",
		detail_remove	: ROOT+"/mall_admin/detail_remove",
		detail_release	: ROOT+"/mall_admin/detail_release",
		detail_range	: ROOT+"/mall_admin/detail_range",

	},
	init:function(){
		this.editor.init();
	},
	editor:{
		init:function(){
			this.label.init();
			this.config.init();
			this.brand.init();
			this.detail.init();
		},
		label:{
			init:function(){
				//启动滑块
				$(window).on('bc-rank-success','#editor-label-rank' , function(event) {
					$('#editor-label-rank-line').trigger('change')
					$(this).find('.bc-rank-content labels').each(function(index, el) {
						var r = $(this)	;
						Sortable.create(r.get(0),{
							group: "label",
							animation: 150,
							scrollSensitivity: 100,
							handle: '.i-move',
						})
						r.append('<label class="editor-label-edit editor-label-new"><div>+</div></label>')
					});
					Sortable.create($('#editor-label-rank-line').get(0),{
						group: "label-line",
						animation: 150,
						scrollSensitivity: 100,
						handle: '.i-menu',
					})
				});
				//编辑按钮
				$(window).on('click', '#editor-label-edit-btn',function(event) {
					var b = $('#editor-label-edit-btn');
					if(b.hasClass('active')){
						b.removeClass('active');
						b.text('编辑');
						b.trigger('editor-label-save')
						$('.editor-label-edit').hide();
						$('.editor-label-content').fadeIn();
					}else{
						b.addClass('active');
						b.text('完成');
						$('.editor-label-edit').fadeIn();
						$('.editor-label-content').hide();
					}
				});
				$(window).on('click', '.editor-label-new', function(event) {
					var self = $(this)
					var t = self.siblings().first().prop('outerHTML');
					self.before(t);
					self.prev().fadeIn();
				});
				$(window).on('click', '.editor-label-remove', function(event) {
					$(this).parents('label').fadeOut(150,function(){
						$(this).remove();
					})
				});
				
				$(window).on('click', '#editor-label-line-add', function(event) {
					var self = $('#editor-label-rank-line')
					var t = self.children().first().prop('outerHTML');
					self.append(t);
					var line = self.children().last()
					line.fadeIn();
					line.addClass('bc-rank-content');
					line.addClass('active');
					line.removeClass('bc-rank-template');
					line.find('labels').children().hide();
					line.find('labels').append('<label class="editor-label-edit editor-label-new"><div>+</div></label>');
					line.find('.editor-label-edit').show();
					line.find('.editor-label-remove').fadeIn()
					Sortable.create(line.find('labels').get(0),{
						group: "label",
						animation: 150,
						scrollSensitivity: 100,
						handle: '.i-move',
					})
				});
				$(window).on('click', '.editor-label-line-remove', function(event) {
					$(this).parents('.bc-rank-content').fadeOut(150,function(){
						$(this).remove();
					})
				});
				post.init(window,{
					method:'editor-label-save',
					input:function(){
						var v = [];
						// console.log($('#editor-label-rank-line .bc-rank-content'))
						$('#editor-label-rank-line .bc-rank-content').each(function(index, el) {
							v.push({category_cn:$(el).find('[name="category_text"]').val().trim(),labels:[]})
							$(el).find('labels').children().each(function(index2, el2) {
								var e = $(el2)
								var lb = e.find('[name="label_text"]');
								if(index2 && lb.length){
									// console.log(lb,lb.val(),{label_cn:lb.val()},v[index]);
									v[index].labels.push({label_cn:lb.val().trim()});
								}
							});
						});
						console.log(v);
						return {value:JSON.stringify(v)};
					},
					url:CONSOLE.urls.label_modify,
					text:'保存',
					confirm:true,
					cancel:function(){
						rank.reload('#editor-label-rank');
					},
					success:function(){
						rank.reload('#editor-label-rank');
						rank.reload('#editor-brand-label');
						rank.reload('#editor-brand-detail-config');
					}
				},'#editor-label-edit-btn')

			},
			open:function(){
				var e = $('#editor-panel-label-config');
				e.fadeIn();
				e.siblings().hide();
			}
		},
		config:{
			labelSelected:function(){
				return $('#editor-config-labels').val();
			},
			listReload:function(){
				list.reload('#editor-config-list');
			},
			init:function(){
				var self = this;
				$(window).on('bc-page-success','.page.editor', function(event) {
					Sortable.create($('#editor-config-list-content').get(0),{
						animation: 150,
						scrollSensitivity: 100,
						handle: '.i-menu',
						onUpdate:function(){
							$('#editor-config-list').trigger('editor-config-range')
						}
					})
				})
				//refresh
				$(window).on('change','#editor-label-rank-line', function(event) {
					var c = $('[name$="label-edit"]:checked')
					// var v = [];
					var t = [];
					c.each(function(index, el) {
						// var label_id = $(el).parents('label').find('label_id').html();
						// if(label_id) v.push(label_id);
						var label_text = $(el).parents('label').find('label_text').html();
						if(label_text) t.push(label_text);
					});
					// var v = v.join(',');
					var t = t.join(',');
					if(t == ''){t = '全部'}
					// $("#editor-config-labels").val(v).trigger('change');
					$("#editor-config-labels_text").html(t);
				});
				//edit button
				$(window).on('click','#editor-config-edit-btn', function(event) {
					var b = $('#editor-config-edit-btn');
					if(b.hasClass('active')){
						b.removeClass('active');
						b.text('编辑');
						$('.editor-config-edit').hide();
						$('.editor-config-content').fadeIn();
						self.listReload();
					}else{
						$('#editor-config-list .bc-list-content config_options>label').addClass('editor-config-move');
						b.addClass('active');
						b.text('完成');
						$('.editor-config-edit').fadeIn();
						$('.editor-config-content').hide();
					}
				});
				//range
				post.init(window,{
					method:"editor-config-range",
					input:function(){
						var range = [];
						$('#editor-config-list .bc-list-content').children().each(function(index, el) {
							range.push($(el).attr('data-id'))
						});
						range = range.join(',');
						return {range:range}
					},
					url:CONSOLE.urls.config_range,
					style:'none',
				},'#editor-config-list')
				//add
				post.init(window,{
					input:function(){
						var labels = self.labelSelected();
						return {labels:labels}
					},
					text:'添加',
					style:'none',
					success:function(){
						self.listReload();
					},
					url:CONSOLE.urls.config_create
				}, '#editor-config-create')
				//release
				post.init(window,{
					method:'change',
					stop:function(e){
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						if(!config_id){
							return true;
						}
					},
					input:function(e){
						var e = $(e);
						var release = parseInt(e.prop('checked')*1) || 0;
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						return {config_id:config_id,release:release}
					},
					style:'input',
					url:CONSOLE.urls.config_release
				},'.editor-config-release-switch')
				//newname
				post.init(window,{
					method:'change',
					stop:function(e){
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						if(!config_id){
							return true;
						}
					},
					text:'重命名',
					input:function(e){
						var e = $(e);
						var newname = e.val();
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						return {config_id:config_id,newname:newname}
					},
					style:'input',
					url:CONSOLE.urls.config_rename,
					success:function(e){
						$(e).parents('.bc-list-line').find('config_name_cn').html($(e).val())
					}
				},'.editor-config-name-input')
				//remove
				post.init(window,{
					stop:function(){
						var config_id = list.selected('#editor-config-list');
						if(!config_id.length){
							return true;
						}
					},
					confirm:function(){
						return {target:list.selected('#editor-config-list').length+"个配置"}
					},
					text:'删除',
					input:function(){
						var config_id = list.selected('#editor-config-list');
						config_id = config_id.join(',')
						return {config_id:config_id}
					},
					url:CONSOLE.urls.config_remove,
					success:function(){
						self.listReload();
					}
				},'#editor-config-remove')
				//option_remove
				post.init(window,{
					stop:function(e){
						var config_option_id = e.parents('label').find('config_option_id').html();
						if(!config_option_id || config_option_id==''){
							return true;
						}
					},
					text:'删除',
					input:function(e){
						var e = $(e);
						var config_option_id = e.parents('label').find('config_option_id').html();
						return {config_option_id:config_option_id}
					},
					style:'none',
					success:function(e){
						var e = $(e).parents('label');
						e.fadeOut(120,function(){
							e.remove();
						})
					},
					url:CONSOLE.urls.config_option_remove,
				},'.editor-config-option-remove')
				post.init(window,{
					stop:function(e){
						var config_id = e.parents('.bc-list-line').attr('data-id');
						if(!config_id || config_id==''){
							return true;
						}
					},
					text:'清空',
					confirm:true,
					input:function(e){
						var e = $(e);
						var config_id = e.parents('.bc-list-line').attr('data-id');
						return {config_id:config_id}
					},
					success:function(e){
						var e = $(e).siblings('config_options').children().first().siblings();
						e.fadeOut(120,function(){
							e.remove();
						})
					},
					url:CONSOLE.urls.config_option_clean,
				},'.editor-config-option-clean')
				//option_add
				post.init(window,{
					method:'change',
					stop:function(e){
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						if(!config_id){
							return true;
						}
					},
					text:'添加',
					style:'none',
					input:function(e){
						var e = $(e);
						var config_option_name = e.val();
						e.val('');
						var config_id = e.parents('.bc-list-line').find('config_id').html();
						return {config_id:config_id,config_option_name:config_option_name}
					},
					// style:'input',
					success:function(e,res){
						var e = $(e).parents('.bc-list-line').find('config_options').eq(0);
						var v = res.data.config_options;
						// var v = [{},{},{},{}]
						console.log(e,v)
						value.write(e,'list',v);
					},
					url:CONSOLE.urls.config_option_create,
				},'.editor-config-option-create')
				//option_range
				$(window).on('bc-list-success', '#editor-config-list', function(event) {
					$(this).find('.bc-list-content').children().each(function(index, el) {
						Sortable.create($(el).find('config_options').get(0),{
							animation: 150,
							scrollSensitivity: 100,
							handle: '.editor-config-move',
							onUpdate:function(evt){
								$(evt.item).parents('config_options').trigger('editor-config-option-range');
							}
						})
					});
				});
				post.init(window,{
					method:'editor-config-option-range',
					style:'none',
					stop:function(e){
						var config_id = $(e).parents('.bc-list-line').attr('data-id');
						if(!config_id) return true;
					},
					input:function(e){
						var line = $(e).parents('.bc-list-line')
						var config_id = line.attr('data-id');
						var range = [];
						line.find('config_options').children().each(function(index, el) {
							if(index) range.push($(el).find('config_option_id').html())
						});
						range = range.join(',');
						return {config_id:config_id,range:range};
					},
					url:CONSOLE.urls.config_option_range,
				},'#editor-config-list config_options')
			}
		},
		brand:{
			init:function(){
				var self = this;
				//
				post.init(window,{
					input:function(){
						var labels = $('#editor-brand-list-labels-input').val();
						return {labels:labels};
					},
					text:'添加',
					success:function(){
						list.reload('#editor-brand-list')
					},
					url:CONSOLE.urls.brand_create
				},"#editor-brand-create")
				//remove
				post.init(window,{
					stop:function(){
						return list.selected('#editor-brand-list').length <=0;
					},
					input:function(){
						var mall_id = list.selected('#editor-brand-list');
                        mall_id = mall_id.join(',');
						return {mall_id:mall_id};
					},
					text:'删除',
					confirm:function(){
						return {target:list.selected('#editor-brand-list').length+"个产品"}
					},
					success:function(){
						list.reload('#editor-brand-list')
					},
					url:CONSOLE.urls.brand_remove
				},"#editor-brand-remove")
				//modify
				post.init(window,{
					method:'change',
					url:CONSOLE.urls.brand_modify,
					style:'input',
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
						if(!mall_id) return true;
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var key = $(e).attr('name');
						var content = $(e).val();
						return {mall_id:mall_id,key:key,content:content}
					},
					success:function(){
						list.reload('#editor-brand-list')
					}
				},'.editor-brand-modify')
				//release
				post.init(window,{
					method:'change',
					url:CONSOLE.urls.brand_release,
					style:'input',
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
						if(!mall_id) return true;
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var release = parseInt($(e).prop('checked')*1) || 0;
						return {mall_id:mall_id,release:release}
					},
					success:function(){
						list.reload('#editor-brand-list')
					}
				},'#editor-brand-released')
			},
			open:function(e){
				$('#editor-panel-label-config').hide();
				$('#editor-panel-brand-detail').fadeIn();
				detail.put($('#editor-panel-brand-detail'),$(e).find('.bc-list-detail-box'));
				$(window).scrollTop(0)
			}
		},
		detail:{
			init:function(){
				//create
				post.init(window,{
					stop:function(){
						var mall_id = $("#option-brand-detail-mall_id").html();
						if(!mall_id) return true;
						var price = parseInt($('#editor-detail-price-input').val()*100);
						if(!price){
							alert('请填写正确的价格')
							$('#editor-detail-price-input').select();
							$('#editor-detail-price-input').focus();
							return true;
						}
					},
					input:function(e){
						var mall_id = $("#option-brand-detail-mall_id").html();
						var price = parseInt($('#editor-detail-price-input').val()*100);
						var code = $('#editor-detail-code-input').val();
						var options = {};
						$('#editor-detail-config-list .bc-list-content').children().each(function(index, el) {
							var c = $(el).attr('data-id');
							var v = $(el).find('.editor-config-option').val();
							if(c && v){
								if(!options[c]) options[c] = {};
								options[c]['option_name_cn'] = v;
							}
						});
						options = JSON.stringify(options);
						return {mall_id:mall_id,price:price,code:code,options:options}
					},
					text:'添加',
					url:CONSOLE.urls.detail_create,
					success:function(){
						list.reload('#editor-detail-list');
					}
				},'#editor-detail-create')
				$(window).on('change','#editor-detail-config-list .weui-select',function(){
					var e = $(this);
					var v = e.val();
					console.log(e,v)
					e.parents('.bc-list-line').find('.editor-config-option').val(v);
					e.val('')
				})
				$(window).on('bc-list-success', '#editor-detail-config-list', function(event) {
					$(this).find('.bc-list-content').children().each(function(index, el) {
						var options = $(el).find('config_options').html();
						options = eval("("+options+")");
						for(var i in options){
							var option = options[i]['option_name_cn'];
							$(el).find('.weui-select').append('<option>'+option+'</option>')
						}
					});
				});
				//remove
				post.init(window,{
					stop:function(){
						var detail_id = list.selected('#editor-detail-list');
						if(!detail_id.length){
							return true;
						}
					},
					confirm:function(){
						return {target:list.selected('#editor-detail-list').length+"个价格"}
					},
					text:'删除',
					input:function(){
						var detail_id = list.selected('#editor-detail-list');
						detail_id = detail_id.join(',')
						return {detail_id:detail_id}
					},
					url:CONSOLE.urls.detail_remove,
					success:function(){
						list.reload('#editor-detail-list');
					}
				},'#editor-detail-remove')
				//release
				post.init(window,{
					input:function(e){
						var detail_id = $(e).parents('.bc-list-line').attr('data-id');
						var release = parseInt($(e).prop('checked')*1) || 0;
						return {detail_id:detail_id,release:release}
					},
					stop:function(e){
						var detail_id = $(e).parents('.bc-list-line').attr('data-id');
						if(!detail_id) return true;
					},
					style:'input',
					url:CONSOLE.urls.detail_release,
				},".editor-detail-release")
				//range
				$(window).on('bc-list-success', '#editor-detail-list', function(event) {
					Sortable.create($('#editor-detail-list .bc-list-content').get(0),{
						animation: 150,
						scrollSensitivity: 100,
						handle:".i-menu",
						onUpdate:function(){
							$('#editor-detail-list').trigger('editor-detail-range')
						}
					})
				});
				post.init(window,{
					method:"editor-detail-range",
					input:function(){
						var range = [];
						$('#editor-detail-list .bc-list-content').children().each(function(index, el) {
							range.push($(el).attr('data-id'))
						});
						range = range.join(',');
						return {range:range}
					},
					url:CONSOLE.urls.detail_range,
					style:'none',
				},'#editor-detail-list')
			}
		}
	},
	cashier:{
		init:function(){

		},
		selectDetail:function(t){
			$('#mall-menu-list').hide();
			$('#mall-menu-detail').fadeIn();
		}
	}
}
CONSOLE.init();

//old
//old
//old
//old
//old
//old
//old
/*cafe_order_id = null;
cafe = {
	detail:function(){//展开detail
		var p = $("#admin-cafe-editor-detail").parent();
		p.siblings().hide();
		if(p.css('display') == 'none'){
			p.fadeIn();
		}
		$("[name='record-box']").first().prop("checked",true);
		detail.put("#admin-cafe-editor-detail");
	},
	init_pay:function(){
		//会员卡号
		//会员卡号
		//会员卡号
		$(window).on('change',"#member-card-code", function(event) {
			var detail = cart.detail();
			if(!detail){
				$("#member-card-code").val('')
				alert('清单为空');
				return
			}
			var card_code = $(this).val();
			if( !card_code || (card_code.length<8)){
				$("#member-card-code").val('')
				alert('会员卡号不正确');
				return;
			}
			detail = JSON.stringify(detail);
			info.reload("#cafe-order-info",{detail:detail,card_code:card_code})
		});
		$(window).on('info-success',"#cafe-order-info", function(event) {
			$(this).fadeIn();
			var total_fee = $(this).find('total_fee').html();
			value.write($("#admin-cafe-cart-total_fee"),'ss',total_fee);
			// $("#member-card-code").attr("disabled",true);
			$("order-pay-auth_code").focus();
			// modal.resize("#admin-book-cashier-modal");
			setTimeout(function(){
				$("#cafe-order-info").trigger("info-success-pay-start");
			},1500);
		});
		$(window).on('info-error',"#cafe-order-info", function(event,errormsg) {
			$("#member-card-code").val('')
			post.animate.error({errormsg:errormsg})
		})
		//会员卡支付
		//会员卡支付
		//会员卡支付
		//会员卡支付
		post.init(window,{
			method:"info-success-pay-start",
			stop:function(){
				if(cafe_order_id){
					$(window).trigger('account-pay-start');
				}
				var detail = cart.detail();
				if(!detail){
					return true
				}
				return false;
			},
			url:"/cafeadmin/order_create",
			input:function(){
				var detail = cart.detail();
				var card_code = $("#member-card-code").val();
				detail = JSON.stringify(detail);
				return {card_code:card_code,detail:detail}
			},
			text:"生成订单",
			confirm:function(){
				// var member_available = $("member_available").html();
				var amount = $("#admin-order-total_fee").html();
				return {target:amount,text:"使用会员卡支付"}
			},
			success:function(e,ret){
				cafe_order_id = ret.data.info.order_id;
				$(window).trigger('account-pay-start');
			}
		},"#cafe-order-info")
		post.init(window,{
			method:"account-pay-start",
			url:"/pay/account",
			input:function(){
				var auth_code = $("#member-card-code").val();
				return {order_id:cafe_order_id,auth_code:auth_code}
			},
			text:"会员卡支付",
			success:function(){
				success_action();
			},
			msg:{ 
				"account balance not enough":"会员卡余额不足"
			}
		})
		$("#book-order-info").on('info-error', function(event,reason) {
			$("#admin-order-total_fee").html(price_original);
			$(this).find('.member').hide();
			post.animate.error({errormsg:reason})
			$("#member-card-code").removeAttr('disabled');
			$("#member-card-code").val('')
		});
		//付款 获取订单
		//付款 获取订单
		//付款 获取订单
		post.init(window,{
			stop:function(){
				if(cafe_order_id){
					$(window).trigger('admin-pay-start');
				}
				var detail = cart.detail();
				if(!detail){
					return true
				}
				return false;
			},
			url:"/cafeadmin/order_create",
			input:function(){
				var detail = cart.detail();
				var card_code = $("#member-card-code").val();
				detail = JSON.stringify(detail);
				return {card_code:card_code,detail:detail}
			},
			text:"生成订单",
			confirm:function(){
				// var member_available = $("member_available").html();
				var amount = $("#admin-cafe-cart-total_fee").html();
				return {target:amount,text:"现金支付"}
			},
			success:function(e,ret){
				cafe_order_id = ret.data.info.order_id;
				$(window).trigger('admin-pay-start');
			}
		},'#order-pay-admin')
		post.init(window,{
			method:"admin-pay-start",
			url:"/pay/admin",
			input:function(){
				return {order_id:cafe_order_id}
			},
			text:"现金支付",
			success:function(){
				success_action();
			}
		})
		success_action = function(){
			cafe_order_id = null;
			modal.close(".modal");
			info.reload("#admin-cafe-stastics")
			list.reload("#admin-cafe-order-list-today");
			list.reload("#admin-cafe-order-refund-today");
			cart.clean();
		}
		//wx支付
		//wx支付
		//wx支付
		post.init(window,{
			method:"wxpay-start",
			url:"/pay/wx_scan",
			input:function(){
				var auth_code = book_order_auth_code;
				book_order_auth_code = null;
				var body = "[未言咖啡]“"+$("#admin-cafe-cashier-cart-list heading").html()+"”等共"+$("#admin-cafe-cart-total-amount").html()+"份";
				return {order_id:cafe_order_id,auth_code:auth_code,body:body}
			},
			text:"微信支付",
			success:function(){
				success_action();
			},
			error:function(e,reason){
				switch(reason){
					case "USERPAYING":
					$("#order-pay-user_paying").fadeIn();
					$("#order-pay-user_paying").siblings().hide();
					var order_id = $("#order-pay-order_id").html();
					recharge_query(order_id);
				}
			},
			msg:{
				"AUTH_CODE_INVALID":"二维码错误",
				"USERPAYING":"需要用户输入密码",
			}
		})
		var recharge_query = function($order_id){
			var p = $("#order-pay-user_paying");
			if(!cafe_order_id || p.parents("modal").css('display') == 'none') return;
			var d = p.find("dot").html();
			var dd = d.length>5?"":d+".";
			p.find("dot").html(dd);
			var order_id = cafe_order_id;
			$.ajax({
				url: '/pay/query',
				type: 'POST',
				data: {order_id: order_id},
				success:function(ret){
					if(ret.state){
						toast.success("充值成功",1);
						success_action('recharge');
					}else{
						setTimeout(function(){
							recharge_query(order_id);
						},500)
					}
				}
			})
		}
		post.init(window,{
			method:'change',
			stop:function(){
				if(cafe_order_id){
					$(window).trigger('wxpay-start');
				}
				var auth_code = $("#order-pay-auth_code").val();
				var detail = cart.detail();
				if(!detail || !auth_code){
					return true
				}
				return false;
			},
			url:"/cafeadmin/order_create",
			input:function(){
				var detail = cart.detail();
				var card_code = $("#member-card-code").val();
				detail = JSON.stringify(detail);
				return {card_code:card_code,detail:detail}
			},
			text:"生成订单",
			success:function(e,ret){
				cafe_order_id = ret.data.info.order_id;
				var auth_code = $("#order-pay-auth_code").val();
				book_order_auth_code = auth_code//$("#order-pay-auth_code").val();
				$("#order-pay-auth_code").val('')
				$(window).trigger('wxpay-start');
			},
			error:function(){
				$("#order-pay-auth_code").val('')
			},
		},'#order-pay-auth_code')
	},
	init_refund:function(){
		$(window).on('modal-start',"#order-pay-refund-modal",function(){
			var self = $(this)
			var refund_available = parseInt(self.find("total_fee").html())-parseInt(self.find("refund").html());
			refund_available = ((refund_available/100).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');;
			var placeholder = " 该订单最大退款金额： "+refund_available;
			// alert(refund_available);
			$("#order-pay-refund-fee").attr("placeholder",placeholder)
			$("#order-pay-refund-fee").val('')
			setTimeout(function(){
				$("#order-pay-refund-fee").focus();
			},10)
		})
		post.init(window,{
			url:"/pay/refund",
			stop:function(){
				var refund_fee = parseInt(parseFloat($("#order-pay-refund-fee").val())*100)
				var order_id = parseInt($("#account-refund-order_id").html())
				if(!refund_fee || !(refund_fee>0) || !order_id){
					return true;
				}
				return false;
			},
			text:"退款",
			input:function(){
				var refund_fee = parseInt(parseFloat($("#order-pay-refund-fee").val())*100)
				var order_id = parseInt($("#account-refund-order_id").html())
				return {order_id:order_id,refund_fee:refund_fee}
			},
			confirm:function(){
				var refund_fee = parseInt(parseFloat($("#order-pay-refund-fee").val())*100)
				var target = ((refund_fee/100).toFixed(2) + '').replace(/\d{1,3}(?=(\d{3})+(\.\d*)?$)/g, '$&,');
				return {target:target}
			},
			success:function(){
				success_action();
			},
			msg:{
				"refund_fee not available":"请勿超出付款金额",
				"order already refunded":"该订单已经全额退款"
			}
		},"#order-pay-refund-confirm")
	},
	init_cashier:function(){
		//筛选
		$(window).on('change','#admin-cafe-menu-labels',function(){
			var l = $('#admin-cafe-cashier-menu-list-content');
			var lbs = $(this).val();
			// console.log(lbs);
			if(!lbs || lbs.split(",").length == 0 ){
				l.children().each(function(index, el) {
					var e = $(el);
					if(e.css("display") == 'none'){
						e.fadeIn(400);
					}	
				});
				return true;
			}
			lbs = lbs.split(",")
			l.children().each(function(index, el) {
				var e = $(el);
				var labels = e.find('labels').html();
				if(!labels || labels == ""){
					return true;
				}
				labels = labels.split(",");
				// console.log(labels);
				var isHide = (e.css('display') == 'none');
				var needHide = false;
				for( var i in lbs){
					var exist = false;
					var needexist = lbs[i];
					for(var j in labels){
						var label = labels[j];
						if(label == needexist){
							exist = true;break;
						}
					}
					if(!exist){
						needHide = true;break;
					}
				}
				if(needHide && !isHide) e.fadeOut(400);
				if(isHide && !needHide) e.fadeIn(400);
			});
		})
		//预设
		$(window).on('list-success', '#admin-cafe-cashier-menu', function(event) {
			$("#admin-cafe-menu-labels-rank input").prop("checked",false);
			$("#admin-cafe-menu-labels-rank input.btn-all").prop("checked",true);
			var self = $(this).find(".bc-list-content");
			self.children().each(function(index, el) {
				var e = $(el);
				var detail_amount = e.find('detail_amount');
				if(parseInt(detail_amount.html()) == 1){
					var detail = e.find("detail").html();
					// var config = e.find("config").html();
					detail = eval(detail)[0];
					// config = eval("("+config+")");
					var detail_id = detail.detail_id;
					var price = detail.price;
					// price = detail.price;
					// console.log(detail,config)
					// return true
					// var detail_id = e.find("detail_id").last().html();
					detail_amount.next("detail_id").html(detail_id);
					detail_amount.next("detail_id").before(" #")
					detail_amount.next().next("price").html(price)
				}
			});
		});
		//模态框选择detail
		$(window).on('modal-success',"#admin-cafe-cashier-menu-modal",function(){
			var mod = $(this);
			var detail = mod.find('detail').html();
			var config = mod.find('config').html();
			var template = $("#admin-cafe-cashier-menu-modal-template");
			var template_html = template.html();
			var template_option = $($("#admin-cafe-cashier-menu-modal-option-template").prop('outerHTML'));
			var template_option_html = template_option.removeAttr('id').removeClass('hidden').prop('outerHTML');
			var container = $("#admin-cafe-cashier-menu-modal-content");
			container.html('');
			detail = eval(detail);
			config = eval("("+config+")");
			// console.log(template,template_html)
			console.log(config,detail);
			for( var i in config ){
				var t = $(template_html);
				t.find("config_id").html(i);
				t.find("config_name").html(config[i]);
				container.append(t);
				var newline = container.children().last();
				for(var j in detail){
					var options = detail[j].options;
					for(var n in options){
						if(n == i){
							var name = options[n];
							// console.log(newline.find("[data-name='"+name+"']"));
							if(newline.find("[data-name='"+name+"']").length){continue;}
							var tmp_option = $(template_option_html);
							tmp_option.attr("data-name",name);
							tmp_option.find("option_name").html(name);
							tmp_option.find('input').attr("name","config_id_"+i)
							newline.find(".admin-cafe-cashier-menu-modal-option-container").prepend(tmp_option);
						}
					}				
				}
			}
			modal.resize(mod);
		})
		$(window).on('click', '.admin-cafe-cashier-menu-modal-label', function(event) {
			event.preventDefault();
			var btn = $(this);
			if(btn.find(".weui-btn_disabled").length) return;
			// if($(this).find("input").prop('checked')){
				btn.find("input").prop('checked',!btn.find("input").prop('checked'))
				var container = btn.parents("#admin-cafe-cashier-menu-modal-content");
			// console.log(container);
			var checked = container.find('input:checked')
			//计算出所有已选项目selected
			var selected = {}
			checked.each(function(index, el) {
				var line = $(el).parents(".admin-cafe-cashier-menu-modal-config")
				var config_id = line.find("config_id").html();
				var name = $(el).next().find("option_name").html();
				selected[config_id] = name
				// console.log($.inArray(name, options))
			});
			//计算出所有已选detail
			var detail = $("#admin-cafe-cashier-menu-modal detail").html()
			detail = eval(detail);
			var detail_selected = [];
			for ( var i in detail) {
				var s = true;
				var options = detail[i].options;
				for(var j in selected){
					s = s && (selected[j] == options[j]);
					if(!s) break;
					// if( options[j] )
				}
				if(s){
					detail_selected.push(detail[i]);
				}
			}
			//计算出所有可用的option
			var options_allowed = {}
			var price_max = null;
			var price_min = null;
			for ( var i in detail_selected) {
				var price = detail_selected[i].price;
				if(!price_max || price>price_max) price_max = price;
				if(!price_min || price<price_min) price_min = price;
				var options = detail_selected[i].options;
				for( var j in options){
					options_allowed[options[j]] = true;
				}
			}
			var labels = container.find(".admin-cafe-cashier-menu-modal-label");
			labels.each(function(index, el) {
				var label = $(el)	
				var name = label.attr("data-name");
				if(options_allowed[name]){
					label.find(".weui-btn").removeClass('weui-btn_disabled');
				}else{
					// if(label.siblings().find("input:checked").length) return true;
					label.find(".weui-btn").addClass('weui-btn_disabled');
				}
			});
			var md = $('#admin-cafe-cashier-menu-modal');
			// console.log(detail_selected,price_max,price_min)
			if(detail_selected.length>1){
				md.find("price").hide();
				md.find("detail_id").hide();
				md.find("price_min,price_max").show();
				value.write(md.find("price_max"),'modal',price_max);
				value.write(md.find("price_min"),'modal',price_min);
				$("#admin-cafe-cashier-menu-modal-add").addClass('disabled')
				md.find("detail_id").html('')
			}else{
				$("#admin-cafe-cashier-menu-modal-add").removeClass('disabled')
				var price = detail_selected[0].price;
				var detail_id = detail_selected[0].detail_id;
				var options = []
				console.log(detail_selected[0].options);
				for(i in detail_selected[0].options){
					options.push(detail_selected[0].options[i]);
				}
				var detail_desc = options.join(",");
				// alert(detail_desc);
				// for(var i in options)
				md.find("price").show();
				md.find("detail_id").show();
				md.find("price_min,price_max").hide();
				md.find("price").each(function(index, el) {
					value.write($(el),'ss',price_min);					
				});
				value.write(md.find("detail_id"),'ss',detail_id);
				md.find("detail_desc").html(detail_desc);
			}
		});
		$(window).on('click', '#admin-cafe-cashier-menu-modal-add:not(.disabled)', function(event) {
			var md = $('#admin-cafe-cashier-menu-modal');
			var detail_id = $(this).find('detail_id').html();
			var price = md.find('price').html();
			var heading = md.find('heading').html();
			var detail_desc = md.find('detail_desc').html();
			cart.add({detail_id:detail_id,price:price,heading:heading,detail_desc:detail_desc})
		});
		//添加
		$(window).on('click', '#admin-cafe-cashier-menu .bc-list-line', function(event) {
			var self = $(this);
			var detail_id_simple = self.find(".menu-detail-simple").html();
			if(detail_id_simple){
				var price = parseInt(self.find("price").html());
				var heading = self.find("heading").html();
				cart.add({detail_id:detail_id_simple,price:price,heading:heading})
				// sef.
				return
			}
			modal.open("#admin-cafe-cashier-menu-modal",self);
		});
		//删除
		$(window).on('click', '#admin-cafe-cashier-cart-list .admin-cafe-piece', function(event) {
			cart.remove($(this))
		})
		//清理
		$(window).on('click', '#admin-cafe-cart-clean', function(event) {
			cart.clean();
		});
	},
	init_labels:function(){
		//选择
		// $("#cafe-category-new-name").focus();
		//重载
		$("#cafe-category-reload").on('click', function(event) {
			rank.reload("#cafe_category_edit");
		});
		//滑动
		Sortable.create(cafe_category_edit, {
			animation: 150,
			scrollSensitivity: 100,
			handle:".cafe-category-move",
			group:"cafe-category"
		});
		$("#cafe_category_edit").on('success',function(){
			var newcatogory = $(this).children(".bc-rank-content");
			newcatogory.find(".cafe-category-items").each(function(index, el) {
				Sortable.create(el, {
					animation: 150,
					scrollSensitivity: 100,
					group:"cafe-category-items",
				});
			});
		})
		//删除
		Sortable.create(cafe_category_remove, {
			animation: 150,
			scrollSensitivity: 100,
			group:"cafe-category",
			handle:false,
			onAdd: function (evt) {
				var itemEl = evt.item;
			}
		});
		Sortable.create(cafe_category_remove, {
			animation: 150,
			handle:false,
			scrollSensitivity: 100,
			group:"cafe-category-items",
			onAdd: function (evt) {
				var itemEl = evt.item;
				$(itemEl).remove()
			}
		});
		//新建类型
		$("#cafe-category-new").on('click', function(event) {
			var ipt = $("#cafe-category-new-name")
			var newname = ipt.val();
			if(!newname.length){
				ipt.focus();
				return;
			}
			newname = newname.split(" ")
			newname_cn = newname[0];
			newname_en = newname.length > 1?newname[1]:"",
			ipt.val('')
			var ctt = $("#cafe_category_edit");
			// console.log(ctt);
			var tmp = ctt.find(".bc-rank-template").prop("outerHTML").replace(/bc-rank-template/g,"bc-rank-content");
			ctt.append(tmp)
			var newcatogory = ctt.children().last()
			newcatogory.find(".cafe-category-name").val(newname_cn);
			newcatogory.find("category_en").html(newname_en);
			newcatogory.fadeIn();
			newcatogory.find(".cafe-category-items").each(function(index, el) {
				Sortable.create(el, {
					animation: 150,
					scrollSensitivity: 100,
					group:"cafe-category-items",
				});
			});
		});
		//新增标签
		$("#cafe_category_edit").on('click', '.cafe-label-new', function(event) {
			var itp = $(this).parent().prev().find("input");
			var newlabel = itp.val();
			if(!newlabel || !newlabel.length) return;
			newlabel = newlabel.split(" ")
			newlabel_cn = newlabel[0];
			newlabel_en = newlabel.length > 1?newlabel[1]:""
			var bci = $(this).parent().siblings('.cafe-category-items');
			var tmp = bci.children().first().prop('outerHTML');
			// console.log(bci,tmp)
			// return
			bci.append(tmp)
			var newlb = bci.children().last()
			// console.log(newlb);
			newlb.find("label_cn").html(newlabel_cn)
			newlb.find("label_en").html(newlabel_en)
			newlb.fadeIn()
			itp.val('');
		});
		//英文部分
		$("#cafe-category-en-show").on('click', function(event) {
			$("label_en,category_en").removeClass('hidden').fadeIn();
		});
		$("#cafe-category-en-hide").on('click', function(event) {
			$("label_en,category_en").addClass('hidden');
		});
		//保存修改
		post.init("#cafe-category-save",{
			text:"保存",
			input:function(){
				var r = [];
				$("#cafe_category_edit .bc-rank-content").each(function(index, el) {
					var e = $(el)
					var c = {}
					c['category_cn'] = e.find(".cafe-category-name").val();
					c['category_en'] = e.find("category_en").html();
					var lbs = []
					e.find(".cafe-category-items>*").each(function(index2, el2) {
						if(!index2) return true;
						var ee = $(el2)
						var lb = {}
						lb['label_en'] = ee.find("label_en").html();
						lb['label_cn'] = ee.find("label_cn").html();
						lbs[index2-1] = lb;
					});
					// lbs = JSON.stringify(lbs);
					c['labels'] = lbs
					r[index] = c
				});
				r = {value:JSON.stringify(r)}
				return r
			},
			url:"/cafeadmin/labels_modify",
			confirm:function(){
				return {}
			},
			success:function(){
				$("[data-port='/cafeadmin/labels']").each(function(index, el) {
					rank.reload(el)
				});
				// rank.reload("#cafe_category_edit");
			}

		})
	},
	init_product:function(){
		//create
		post.init(window,{
			stop:function(e){
				var labels = $("#admin-cafe-labels").val()
				if(!labels || !labels.length){
					alert("请选择分类")
					return true;
				}
			},
			text:"添加",
			input:function(e){
				var labels = $("#admin-cafe-labels").val();
				return {labels:labels}
			},
			url:"/cafeadmin/product_create",
			success:function(e){
				list.reload("#admin-cafe-product-list")
			}
		},"#admin-cafe-create")
		//remove
		post.init(window,{
			stop:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				if(!cafe_id || !cafe_id.length){
					return true;
				}
			},
			text:"删除",
			confirm:function(){
				var target = $("#admin-cafe-editor-detail-name-input").val()
				return {target:target}
			},
			input:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				return {cafe_id:cafe_id}
			},
			url:"/cafeadmin/product_remove",
			success:function(e){
				list.reload("#admin-cafe-product-list");
				detail.clean("#admin-cafe-editor-detail");
			}
		},"#admin-cafe-remove")
		//modify
		post.init(window,{
			method:"change",
			style:"input",
			stop:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				if(!cafe_id || !cafe_id.length){
					return true;
				}
				return $(e).val().length == 0;
			},
			confirm:function(){
				return {}
			},
			text:"修改",
			url:"/cafeadmin/product_modify",
			input:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				var key = $(e).attr("name");
				var content = $(e).val();
				return {cafe_id:cafe_id,key:key,content:content}
			},
			success:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				var key = $(e).attr("name");
				value.write($("#admin-cafe-product-list [data-id='"+cafe_id+"'] "+key),'list',$(e).val());
			}
		},".admin-cafe-modify")
		//hgihlight
		post.init(window,{
			method:"change",
			style:"input",
			stop:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				if(!cafe_id || !cafe_id.length){
					return true;
				}
			},
			url:"/cafeadmin/product_highlight",
			input:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				var highlight = ($(e).prop("checked"))*1
				return {cafe_id:cafe_id,highlight:highlight}
			},
			success:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				var key = "highlight";
				var val = ($(e).prop("checked"))*1;
				$("#admin-cafe-product-list [data-id='"+cafe_id+"'] "+key).each(function(index, el) {
					value.write($(el),'list',val)
				});
			}
		},"#admin-cafe-highlight")
		//release
		post.init(window,{
			method:"change",
			style:"input",
			stop:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				if(!cafe_id || !cafe_id.length){
					return true;
				}
			},
			url:"/cafeadmin/product_release",
			input:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				var release = ($(e).prop("checked"))*1
				return {cafe_id:cafe_id,release:release}
			},
			success:function(e){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				var key = "released";
				var val = ($(e).prop("checked"))*1;
				$("#admin-cafe-product-list [data-id='"+cafe_id+"'] "+key).each(function(index, el) {
					value.write($(el),'list',val)
				});
				// value.write($("#admin-cafe-product-list [data-id='"+cafe_id+"'] "+key),'list',$(e).prop("checked"));
			}
		},"#admin-cafe-release")
	},
	init_config:function(){
		//添加
		post.init(window,{
			stop:function(){
				var labels = $("#admin-cafe-config-editor-labels").val();
				if(!labels){					
					// alert("请选择一个类型");return true;
				}
			},
			input:function(){
				var labels = $("#admin-cafe-config-editor-labels").val();
				return {labels:labels}
			},
			success:function(){
				list.reload("#admin-cafe-config-editor");
			},
			url:"/cafeadmin/config_create",
		},"#admin-cafe-config-create")
		//更名
		post.init(window,{
			method:"change",
			stop:function(e){
				var e = $(e)
				var config_id = e.closest('.bc-list-line').find("config_id").html();
				var newname = e.val();
				if(!newname || !config_id) return true;
			},
			input:function(e){
				var e = $(e)
				var config_id = e.closest('.bc-list-line').find("config_id").html();
				var newname = e.val();
				return {newname:newname,config_id:config_id,lang:'cn'}
			},
			style:"input",
			url:"/cafeadmin/config_rename",
		},".admin-cafe-config-name-cn-input")
		post.init(window,{
			method:"change",
			stop:function(e){
				var e = $(e)
				var config_id = e.closest('.bc-list-line').find("config_id").html();
				var newname = e.val();
				if(!newname || !config_id) return true;
			},
			input:function(e){
				var e = $(e)
				var config_id = e.closest('.bc-list-line').find("config_id").html();
				var newname = e.val();
				return {newname:newname,config_id:config_id,lang:'en'}
			},
			url:"/cafeadmin/config_rename",
			style:"input",
		},".admin-cafe-config-name-en-input")
		//release
		post.init(window,{
			method:"change",
			stop:function(e){
				e = $(e).parents(".weui-cell")
				var config_id = e.find("config_id").html();
				if(!config_id){
					return true;
				}
			},
			style:'input',
			input:function(e){
				e = $(e)
				var config_id = e.parents(".weui-cell").find("config_id").html();
				var release = (e.prop("checked"))*1;
				return {config_id:config_id,release:release};
			},
			url:"/cafeadmin/config_release",
		},".admin-cafe-config-release-switch")
		//删除
		//remove
		post.init(window,{
			stop:function(e){
				var ids = list.selected(e);
				if(!ids || !ids.length){
					return true;
				}
			},
			text:"删除",
			input:function(e){
				var ids = list.selected(e);
				var config_ids = ids.join(",");
				return {config_ids:config_ids}
			},
			confirm:function(e){
				var ids = list.selected(e);
				target = ids.length+"项"
				return {target:target}
			},
			success:function(){
				list.reload("#admin-cafe-config-editor")
			},
			url:"/cafeadmin/config_remove",
		},"#admin-cafe-config-remove")
		//添加option
		$(window).on('modal-success', '#admin-cafe-config-option-modal', function(event) {
			$("#admin-cafe-config-option-create-value-input").focus();
		});
		post.init(window,{
			stop:function(){
				var config_id = $("#admin-cafe-config-option-create-config_id").html();
				if(!config_id) return true;
				var val = $("#admin-cafe-config-option-create-value-input").val();
				if(!val) return true;
			},
			input:function(){
				var config_id = $("#admin-cafe-config-option-create-config_id").html();
				var content = $("#admin-cafe-config-option-create-value-input").val();
				return {content:content,config_id:config_id}
			},
			text:"添加",
			success:function(){
				var box = $("#admin-cafe-config-option-create-box");
				var tmp = $("#admin-cafe-config-editor .bc-list-template options").html();
				var content = $("#admin-cafe-config-option-create-value-input").val();
				var $tmp = $(tmp)
				$tmp.find("option_name_cn").html(content)
				$("#admin-cafe-config-option-create-value-input").val('');
				box.append($tmp);
				box.children().last().fadeIn();
				list.reload("#admin-cafe-config-editor")
			},
			url:"/cafeadmin/config_option_create",
		},"#admin-cafe-config-option-create")
		//删除option
		post.init(window,{
			stop:function(e){
				var e = $(e)
				var config_option_id = e.find("config_option_id").html();
				if(!config_option_id){
					return true
				}
			},
			input:function(e){
				var e = $(e)
				var config_option_id = e.find("config_option_id").html();
				return {config_option_id:config_option_id}
			},
			text:"删除",
			confirm:function(e){
				var e = $(e)
				var target = e.find("config_option_name").html();
				return {target:target}
			},
			success:function(e){
				var e = $(e);
				toast.stop();
				e.fadeOut(300,function(){
					e.remove();
				});
			},
			url:"/cafeadmin/config_option_remove",
		},"#admin-cafe-config-editor .admin-cafe-config-option-remove")
	},
	init_detail:function(){
		//预置detail支持
		$(window).on('change', '.admin-cafe-detail-new-select-option', function(event) {
			var self = $(this);
			var val = self.val();
			if(!val) return;
			var input = self.parents(".weui-cell__bd").prev().children('input');
			input.val(val);
			input.focus();
			self.val("")
		});

		//添加detail
		post.init(window,{
			stop:function(){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html()
				if(!cafe_id){
					return true
				}
				var price = parseInt($("#admin-cafe-detail-price-input").val()*100);
				if(!price){
					alert("请输入价格")
					$("admin-cafe-detail-price-input").focus();
					return true
				}
			},
			url:'/cafeadmin/detail_create',
			input:function(){
				var cafe_id = $("#admin-cafe-editor-detail-cafe_id").html();
				var price = parseInt($("#admin-cafe-detail-price-input").val()*100);
				var options = []
				$("#admin-cafe-config-list-content").children().each(function(index, el) {
					var e = $(el)
					var option = e.find(".admin-cafe-detail-option-input").val();
					if(option){
						var config_id = e.find("config_id").html();
						options.push({config_id:config_id,option:option}) 
					}
				});
				options = JSON.stringify(options);
				return {cafe_id:cafe_id,price:price,options:options}
			},
			text:"添加",
			success:function(){
				list.reload("#admin-cafe-detail-list")
			}
		},"#admin-cafe-detail-create")

		//release
		post.init(window,{
			method:"change",
			stop:function(e){
				e = $(e).parents(".weui-cell")
				var detail_id = e.parents("[data-id]").attr("data-id")
				if(!detail_id){
					return true;
				}
			},
			style:'input',
			input:function(e){
				e = $(e);
				var detail_id = e.parents("[data-id]").attr("data-id")
				var release = (e.prop("checked"))*1;
				return {detail_id:detail_id,release:release};
			},
			url:'/cafeadmin/detail_release',
		},".admin-cafe-detail-release-switch")
		//remove
		post.init(window,{
			stop:function(e){
				var ids = list.selected(e);
				if(!ids || !ids.length){
					return true;
				}
			},
			text:"删除",
			input:function(e){
				var ids = list.selected(e);
				var detail_ids = ids.join(",");
				return {detail_ids:detail_ids}
			},
			confirm:function(e){
				var ids = list.selected(e);
				target = ids.length+"项"
				return {target:target}
			},
			success:function(){
				list.reload("#admin-cafe-detail-list")
			},
			url:'/cafeadmin/detail_remove',
		},"#admin-cafe-detail-remove")
	},
	init:function(){
		this.init_pay();
		this.init_cashier();
		this.init_refund();
		// this.init_labels();
		this.init_product();
		this.init_config();
		this.init_detail();
		$(window).on('page-success', '.editor', function(event) {
			cafe.init_labels();
		});
	}
}
// cafe.init();
cart = {
	add:function(detail){
		cafe_order_id = null;
		var price = detail.price;
		if(!price) return;
		var options = detail.options || "";
		var detail_id = detail.detail_id;
		var cart = $("#admin-cafe-cashier-cart-list");
		var exist = cart.children('[data-id="'+detail_id+'"]');
		if(exist && exist.length){
			var amount = parseInt(exist.find("amount").html())
			exist.find("amount").html(amount+1)
			this.count_price();
			return;
		}
		var detail_desc = detail.detail_desc
		var heading = detail.heading;
		var template = $("#admin-cafe-cashier-cart-template");
		var template_html = template.prop("outerHTML");
		var content_new = template.after(template_html);

		content_new.removeClass('hidden').addClass('bc-info').removeAttr('id').attr('data-id',detail_id);
		info.put(content_new,{price:price,detail_id:detail_id,heading:heading,detail_desc:detail_desc})
		$("#admin-cafe-cashier-cart-empty").hide();
		// console.log(detail)
		this.count_price();
	},
	remove:function(self){
		cafe_order_id = null;
		var amount = parseInt(self.find("amount").html())
		if(amount>1){
			self.find("amount").html(amount-1);
			this.count_price();
			return
		}else{
			self.remove();
		}
		var cart = $("#admin-cafe-cashier-cart-list");
		if(cart.children().length<=2){
			$("#admin-cafe-cashier-cart-empty").fadeIn();
		}
		this.count_price();
	},
	clean:function(){
		cafe_order_id = null;
		$("#admin-cafe-cashier-cart-list").children().not("#admin-cafe-cashier-cart-template,#admin-cafe-cashier-cart-empty").remove();
		value.write($("#admin-cafe-cart-total_fee"),'ss',0);
		$("#admin-cafe-cashier-cart-empty").fadeIn();
		$("#cafe-order-info").hide();
		$("#member-card-code").val('');
		this.count_price();
	},
	count_price:function(){
		var total_fee = 0;
		var total_amount = 0;
		$("#admin-cafe-cashier-cart-list").children('.admin-cafe-piece').not("#admin-cafe-cashier-cart-template").each(function(index, el) {
			var e = $(el);
			var price = parseInt(e.find("price").html());
			var amount =parseInt(e.find("amount").html());
			total_amount += amount;
			// console.log(price,amount);
			total_fee += price*amount;
		});
		$("#admin-cafe-cart-total-amount").html(total_amount)
		value.write($("#admin-cafe-cart-total_fee"),'ss',total_fee);
	},
	detail:function(){
		var detail = {};
		$("#admin-cafe-cashier-cart-list").children('.admin-cafe-piece').not("#admin-cafe-cashier-cart-template").each(function(index, el) {
			var e = $(el);
			var detail_id = e.attr("data-id");
			var amount =parseInt(e.find("amount").html());
			detail[detail_id] = amount;

		});
		var count=0; 
		for(var key in detail){ 
			count++; 
		} 
		if(!count) return null;
		return detail;
	}
}
$(window).on('cl1ick', '#receipt', function(event) {
	// alert(123)
	var m = $(this).closest('#order-pay-refund-modal');
	var order = {};
	order.id = m.find("order_id").html();
	order.total_fee = m.find("total_fee").html()/100;
	order.total_fee_original = m.find("total_fee_original").html()/100;
	order.tel = m.find("tel").html();
	order.detail = [];
	m.find("products").children().each(function(index, el) {
		if(index){
			var e = $(el);
			var heading = e.find("name").html();
			var price = e.find("price").html();
			var amount = e.find("amount").html();
			var d = {price:price,amount:amount,heading:heading}
			order.detail.push(d)
		}
	});
	console.log(order);
	receipt(order);
});
receipt = function (order){
	console.log(order)
	if(!order || !order.id || !order.detail || !order.total_fee) return;
	// if(!confirm('确认打印')) return;
	// var order = {
	// 	id:245235253,
	// 	detail:[{
	// 		heading:"产品1",
	// 		amount:5,
	// 		price:5.2,
	// 	},{
	// 		heading:"产品2这个产品名称有点长",
	// 		amount:3,
	// 		price:10.80,
	// 	}],
	// 	total_fee_original:25.8,
	// 	total_fee:120.5,
	// 	member_user_name:'刘盾',
	// 	discount:0.88,
	// 	table:01,
	// }
	try{ 
		var LODOP = getLodop();
	}catch(err){ 
		alert("无法打印");
		console.error(err);
		return;
	}
	LODOP.PRINT_INIT("cafe");
	LODOP.SET_PRINT_STYLE('FontName','myriad')
	var index = 1
	//header
	var left = 56//$("#width1").val();
	LODOP.ADD_PRINT_TEXT(15,left,355,30,"未言咖啡");
	LODOP.SET_PRINT_STYLEA(index,"FontSize",13);
	LODOP.SET_PRINT_STYLEA(index,"Bold",1);
	index ++;
	LODOP.ADD_PRINT_TEXT(35,left-7,355,20,"Weyard Cafe");
	LODOP.SET_PRINT_STYLEA(index,"FontSize",10);
	LODOP.SET_PRINT_STYLEA(index,"Bold",1);
	index ++;
	var h = 55;
	LODOP.ADD_PRINT_LINE(h,0,h+1,173,0,1);
	index ++;
	//order_id
	h += 5
	LODOP.ADD_PRINT_TEXT(h,0,355,15,"订单号："+order.id);
	index ++;
	//detail
	h += 25
	var d = order.detail
	for(var i in d){
		var heading = d[i].heading;
		// heading = heading.substring(0,7);
		LODOP.ADD_PRINT_TEXT(h,0,100,15,heading);
		index ++;
		LODOP.ADD_PRINT_TEXT(h,100,40,15,"¥");
		index ++;
		LODOP.ADD_PRINT_TEXT(h,100,40,15,d[i].price);
		LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
		index ++;
		LODOP.ADD_PRINT_TEXT(h,145,20,15,"×");
		index ++;
		LODOP.ADD_PRINT_TEXT(h,153,20,15,d[i].amount);
		LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
		index ++;
		h += 18
	}
	
	h += 25
	LODOP.ADD_PRINT_TEXT(h,0,355,15,"总金额");
	LODOP.SET_PRINT_STYLEA(index,"FontSize",12);
	LODOP.SET_PRINT_STYLEA(index,"Bold",1);
	index ++;
	LODOP.ADD_PRINT_TEXT(h,113,60,15,order.total_fee);
	LODOP.SET_PRINT_STYLEA(index,"FontSize",12);
	LODOP.SET_PRINT_STYLEA(index,"Bold",1);
	LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
	index ++;
	h += 2
	if( order.total_fee_original && order.tel ){
		h += 18
		LODOP.ADD_PRINT_TEXT(h,0,355,15,"订单原价");
		index ++;
		LODOP.ADD_PRINT_TEXT(h,133,40,15,order.total_fee_original);
		LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
		index ++;
		h += 18
		LODOP.ADD_PRINT_TEXT(h,0,355,15,"会员优惠");
		index ++;
		LODOP.ADD_PRINT_TEXT(h,133,40,15, (order.total_fee - order.total_fee_original));
		LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
		index ++;
		h += 18
		LODOP.ADD_PRINT_TEXT(h,0,355,15,"会员帐户");
		index ++;
		LODOP.ADD_PRINT_TEXT(h,33,140,15,order.tel);
		LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
		index ++;
	}else{
		h += 18
		LODOP.ADD_PRINT_TEXT(h,0,355,15,"未获得会员优惠");
		index ++;
		// LODOP.ADD_PRINT_TEXT(h,33,140,15,order.tel);
		// LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
		// index ++;
	}
	if(order.table){
		h += 18
		LODOP.ADD_PRINT_TEXT(h,0,355,15,"桌号");
		index ++;
		LODOP.ADD_PRINT_TEXT(h,133,40,15,order.table);
		LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
		index ++;
	}
	// LODOP.SET_PRINT_STYLEA(4,"FontSize",8);
	//summary
	h += 18
	LODOP.ADD_PRINT_LINE(h,0,h+1,173,0,1);
	index ++;
	h += 15
	LODOP.ADD_PRINT_TEXT(h,20,173,15,'微信扫描二维码办理会员');
	index ++;
	h += 15
	LODOP.ADD_PRINT_BARCODE(h,10,173,173,"QRCode","http://www.weyard.cn/register/mobile?from=receipt");
	index ++;
	h += 173
	h += 5
	//time
	var date = new Date();
	var year 	= date.getFullYear();
	var month 	= date.getMonth()+1;
	var day 	= date.getDate();
	var hour 	= date.getHours();
	var minute 	= date.getMinutes();
	var second 	= date.getSeconds();
	LODOP.ADD_PRINT_TEXT(h,0,173,15,""+year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second);
	LODOP.SET_PRINT_STYLEA(index,"Alignment",3);
	index ++;

	LODOP.SET_PRINT_PAGESIZE(3,580,10,"");
	if(confirm("直接打印")){
		LODOP.PRINT();
	}else{
		LODOP.PREVIEW();
	}*/
// }